﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Dycox.Reporting
{
    public abstract class TableElement : TablePart
    {
        private Table _table;

        public override Table Table
        {
            get { return _table; }
        }

        internal void SetTable(Table value)
        {
            if (_table != value)
            {
                if (_table != null)
                    _table.NotifyElementRemoved(this);
                _table = value;
                if (_table != null)
                    _table.NotifyElementAdded(this);
            }
        }

        protected override IHasStyle[] GetStyleParents()
        {
            return null;
        }
    }

    public abstract class TablePart : IHasStyle
    {
        private ElementStyle _style;
        [XmlElement("style")]
        public ElementStyle Style
        {
            get { return _style; }
            set { _style = value; }
        }

        public abstract Table Table
        {
            get;
        }

        protected abstract IHasStyle[] GetStyleParents();

        internal ElementStyle GetRuntimeStyle()
        {
            var array = GetStyleParents();
            var style = Table.GetRuntimeStyle();

            if (array != null)
            {
                foreach (var obj in array)
                {
                    if (obj.Style != null)
                        style.OverrideBy(obj.Style);
                }
            }

            if (_style != null)
                style.OverrideBy(_style);

            return style;
        }

        ElementStyle IHasStyle.GetRuntimeStyle()
        {
            return this.GetRuntimeStyle();
        }
    }
}
